package com.zigong.app.ui.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zigong.app.R;

/**
 * Author: create by Administrator on 2018/6/28 15 44
 * Des:
 */
public class SingleToast {

    private static Toast mToast;

    /**
     * 双重锁定，使用同一个Toast实例
     */
    private static Toast getInstance(Context context) {
        if (mToast == null) {
            synchronized (SingleToast.class) {
                if (mToast == null) {
                    mToast = new Toast(context);
                }
            }
        }
        return mToast;
    }

    /**
     * 自定义中间吐司
     *
     * @param context context
     * @param text    信息
     */
    public static void showCenterToast(Context context, String text) {
        if (!TextUtils.isEmpty(text)) {
            View view = LayoutInflater.from(context).inflate(R.layout.toast_view, null);
            TextView infoView = (TextView) view.findViewById(R.id.view_Toast_text);
            infoView.setText(text);
            Toast toast = SingleToast.getInstance(context.getApplicationContext());
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        }
    }

}
